package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.entity.SysRole;
import com.heima.homeservicemanagementsystem.entity.dto.Option;

import com.heima.homeservicemanagementsystem.entity.vo.RoleAuthority;
import com.heima.homeservicemanagementsystem.entity.vo.RoleListVo;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;


/**
 * 角色表;(sys_role)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2024-11-25
 */
@Slf4j
@Tag(name = "角色表对象功能接口")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;


    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<SysRole>> paginQuery(@RequestBody PageParams<SysRole> pageParams) {
        //查询参数
        SysRole params = pageParams.getParams();
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(params.getName() != null, SysRole::getName, params.getName());
        lqw.like(params.getCode() != null, SysRole::getCode, params.getCode());
        return Result.success(sysRoleService.page(pageParams.getPage(), lqw));
    }

    @Operation(summary = "获取角色表格的信息")
    @GetMapping("/getTable")
    public Result gettabel() {

        List<RoleListVo> tableRoles = sysRoleService.getTableRoles();
        log.info("获取角色的信息;{}", tableRoles);

        return Result.success(tableRoles);
    }

    @Operation(summary = "搜索角色相关的信息")
    @GetMapping("/search")
    public Result searchQuery(@RequestParam(required = false) String role, @RequestParam String value) {

        sysRoleService.search(role, value);
        log.info("角色：{}", role.getClass());
        log.info("获取角色的信息;{}", sysRoleService.search(role, value));

        return Result.success(sysRoleService.search(role, value));


    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/roleOptions")
    public Result<List<Option>> roleOptions() {
        //查询参数

        QueryWrapper<SysRole> lqw = new QueryWrapper<>();
        lqw.select("name AS label", "id AS value");
        List<Map<String, Object>> maps = sysRoleService.listMaps(lqw);

        List<Option> optionals = BeanUtil.copyToList(maps, Option.class);
        return Result.success(optionals);
    }


    /**
     * 新增数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增角色")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody SysRole sysRole) {
        log.info("角色信息：{}", sysRole);
        SysRole sysRole1 = new SysRole();

        if (sysRoleService.save(sysRole)) {
            return Result.success();
        }
        return Result.error();

    }

    /**
     * 更新数据
     *
     * @param sysRole 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新角色")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @GetMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {
//        sysRoleService.removeByIds()
        if (sysRoleService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @PostMapping("/delete")
    public Result<Boolean> deleteByIds(@RequestBody List<String> id) {
//        sysRoleService.removeByIds()
        log.info("前端信息：{}", id);

        if (sysRoleService.removeByIds(id)) {
//            sysRoleService.removeMeprebyId(id);
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("getPermission/{roleId}")
    @Operation(summary = "获取角色拥有的所有权限包含菜单",
            parameters = {
                    @Parameter(name = "roleId", description = "角色ID", required = true, in = ParameterIn.PATH)
            })
    public Result<List<String>> getPermission(@PathVariable String roleId) {
        List<String> list = sysRoleService.getPermissions(roleId);
        return Result.success(list);
    }

    /**
     * 角色与权限绑定
     *
     * @param authority
     * @return com.heima.homeservicemanagementsystem.module.Result<java.lang.Boolean>
     * @author jinchengfei
     * @create 2025/2/28
     **/

    @PostMapping("check")
    @Operation(summary = "设置角色拥有的权限接口")
    public Result<Boolean> check(@RequestBody RoleAuthority authority) {
        log.info("菜单信息：{}", authority);
        if (sysRoleService.check(authority)) {
            SysRole role = sysRoleService.getById(authority.getRoleId());
            SaSessionCustomUtil.deleteSessionById("role-"+role.getCode());

            return Result.success(Boolean.TRUE);
        }
        return Result.error();

    }

}